package com.tsj.spzx.controller;

import com.github.pagehelper.PageInfo;
import com.tsj.spzx.model.entity.product.Brand;
import com.tsj.spzx.model.vo.common.Result;
import com.tsj.spzx.model.vo.common.ResultCodeEnum;
import com.tsj.spzx.service.BrandService;
import org.junit.jupiter.api.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
@Tag("品牌管理接口")
@RestController
@RequestMapping("/admin/product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result findByPage(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit){
        PageInfo<Brand> pageInfo = brandService.findByPage(page,limit);
        return  Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 品牌添加
     * @param brand
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Brand brand){
        brandService.save(brand);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 品牌修改
     * @param brand
     * @return
     */
    @PutMapping("/updateById")
    public Result updateById(@RequestBody Brand brand){
        brandService.updateById(brand);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除品牌信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        brandService.deleteById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询所有品牌信息
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<Brand> brandList = brandService.findAll();
        return Result.build(brandList, ResultCodeEnum.SUCCESS);
    }


}
